﻿//
// Created by Administrator on 2022/2/3.
//

#include "NativeLoader.h"

//map001
//gltf
//nodes
//node
//info format
//to json
/*
EBool NativeLoader::loadMap(const EString& mapName)
{
    mCurMap = mapName;

    Log("Loading map #%s...", mapName.c_str());
    //?
    mCollisionValue.clear();
    mCollisionMeshes.clear();
    //??
    std::ifstream in;
    //
    in.open(gPreference->GetMediaPath(mapName).c_str());

    if (in.bad())
    {
        in.close();
        return false;
    }

    char line[256];
    while (in.good())
    {
        EString info;
        in.getline(line, 256);
        info = Trim(line);

        int beg = 0, end = -1;

        if (info.find("Config") != -1)
        {
            // <Config Name="map001">
            EString mapName = getBlock(info, '\"', '\"');

            // <Mesh>Terr_Forest_1</Mesh>
            in.getline(line, 256);
            info = Trim(line);
            EString meshName = getBlock(info, '>', '<') + ".mesh";

            // <Grid X="15" Z="14" Size="10" />
            in.getline(line, 256);

            getXZ(line, mX, mZ, mBlockSize);
            mXL = mX * mBlockSize;
            mZL	= mZ * mBlockSize;
            mHXL = mXL / 2.0f;
            mHZL = mZL / 2.0f;
            // 初始化碰撞位置数据
            for (EInt i = 0; i <= mZ; i++){
                //mCollisionValue.emplace_back(mX + 1, 0);
                //mCollisionMeshes.emplace_back(mX + 1, 0);
                mCollisionValue.push_back( std::vector<EInt>( mX + 1, 0 ) );
                mCollisionMeshes.push_back( std::vector<EMesh*>( mX + 1, 0 ) );
            }

            // </Config>
            in.getline(line, 256);
            //
            mTerrainMesh = mScene->createMesh(mapName, meshName);
            mTerrainMesh->setCullFlag(false);
        }

        in.getline(line, 256);
        info = Trim(line);

        if (info.find("Nodes") != -1)
        {
            in.getline(line, 256);
            info = Trim(line);
            //
            while (info.find("Node") != -1)
            {
                int i = info.find("</Nodes>");
                if (i != -1)
                    break;

                // <Node Name="Tree_3#120">
                EString nodeName = getBlock(info, '\"', '\"');

                // <Mesh>Tree_3</Mesh>
                in.getline(line, 256);
                info = Trim(line);
                EString meshName = getBlock(info, '>', '<') + ".mesh";

                // <Position X="-49.649979" Y="4.247236" Z="-5.005510" />
                in.getline(line, 256);
                info = Trim(line);
                EVector3D pos = getPos(info);

                EMesh *object = mScene->createMesh(nodeName, meshName);
                object->setPosition(pos);

                // <Map X="6" Z="2" Value="0" />
                in.getline(line, 256);
                EInt x, z, value;
                getValue(line, x, z, value);
                if (value >= mCollisionValue[z][x])
                {
                    mCollisionValue[z][x]	= value;
                    mCollisionMeshes[z][x]	= object;
                }

                // </Node>
                in.getline(line, 256);
                in.getline(line, 256);
                info = Trim(line);
            }
        }
    }

    in.close();

    Log("Map Load Succeed!");

    return true;
}
 */